/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import neural.RedeNeural;
import utils.FeatureAssembly;
import utils.Utils;

/**
 *
 * @author Igor
 */
public class JanelaPrincipal extends javax.swing.JFrame {

    int i = 1;
    PlanarImage original;
    PlanarImage imagemCopia;
    public DisplayPrincipal principal = new DisplayPrincipal();
    public DisplayPrincipal auxiliar = new DisplayPrincipal();
    PlanarImage imagemProcessada;
    PlanarImage imagemAuxiliar;
    PlanarImage reconhecida;
    ArrayList<float[]> featureList = new ArrayList<float[]>();
    RedeNeural redeNeural;
    CameraCapture captura = new CameraCapture();

    /**
     * Creates new form JanelaPrincipal
     */
    public JanelaPrincipal() {
        initComponents();
        principal = new DisplayPrincipal();
        auxiliar = new DisplayPrincipal();
        painelPrincipal.add(principal);
        painelPrincipal.repaint();

        painelAuxiliar.add(auxiliar);
        painelAuxiliar.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        AbrirImagem1 = new javax.swing.JButton();
        adicionaPixels = new javax.swing.JButton();
        botaoFeature = new javax.swing.JButton();
        gerarArquivo = new javax.swing.JButton();
        treinaRede = new javax.swing.JButton();
        reconhece = new javax.swing.JButton();
        painelPrincipal = new DisplayPrincipal();
        painelAuxiliar = new DisplayPrincipal();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);

        AbrirImagem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/open.png"))); // NOI18N
        AbrirImagem1.setFocusable(false);
        AbrirImagem1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AbrirImagem1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        AbrirImagem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AbrirImagem1ActionPerformed(evt);
            }
        });
        jToolBar1.add(AbrirImagem1);

        adicionaPixels.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/add.png"))); // NOI18N
        adicionaPixels.setFocusable(false);
        adicionaPixels.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        adicionaPixels.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        adicionaPixels.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionaPixelsActionPerformed(evt);
            }
        });
        jToolBar1.add(adicionaPixels);

        botaoFeature.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/vector.png"))); // NOI18N
        botaoFeature.setFocusable(false);
        botaoFeature.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botaoFeature.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        botaoFeature.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoFeatureActionPerformed(evt);
            }
        });
        jToolBar1.add(botaoFeature);

        gerarArquivo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/arquivo.png"))); // NOI18N
        gerarArquivo.setFocusable(false);
        gerarArquivo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        gerarArquivo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        gerarArquivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gerarArquivoActionPerformed(evt);
            }
        });
        jToolBar1.add(gerarArquivo);

        treinaRede.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/neural.png"))); // NOI18N
        treinaRede.setFocusable(false);
        treinaRede.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        treinaRede.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        treinaRede.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                treinaRedeActionPerformed(evt);
            }
        });
        jToolBar1.add(treinaRede);

        reconhece.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/calculaRede.png"))); // NOI18N
        reconhece.setText("Reconhece");
        reconhece.setFocusable(false);
        reconhece.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        reconhece.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        reconhece.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reconheceActionPerformed(evt);
            }
        });
        jToolBar1.add(reconhece);

        painelPrincipal.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        painelPrincipal.setPreferredSize(new java.awt.Dimension(600, 600));

        javax.swing.GroupLayout painelPrincipalLayout = new javax.swing.GroupLayout(painelPrincipal);
        painelPrincipal.setLayout(painelPrincipalLayout);
        painelPrincipalLayout.setHorizontalGroup(
            painelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 448, Short.MAX_VALUE)
        );
        painelPrincipalLayout.setVerticalGroup(
            painelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 598, Short.MAX_VALUE)
        );

        painelAuxiliar.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout painelAuxiliarLayout = new javax.swing.GroupLayout(painelAuxiliar);
        painelAuxiliar.setLayout(painelAuxiliarLayout);
        painelAuxiliarLayout.setHorizontalGroup(
            painelAuxiliarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 438, Short.MAX_VALUE)
        );
        painelAuxiliarLayout.setVerticalGroup(
            painelAuxiliarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(painelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(painelAuxiliar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(painelPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(painelAuxiliar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AbrirImagem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AbrirImagem1ActionPerformed

        
        System.out.println("Imagem número: "+ i);
        original = JAI.create("fileload", "C:/snapsTirados/" + i + ".png");
        imagemAuxiliar = JAI.create("fileload", "C:/snapsTirados/" + i + "mask.png");
        reconhecida = JAI.create("fileload", "C:/snapsTirados/" + 1 + ".png");
        
        
        principal.setImage(original);
        auxiliar.setImage(imagemAuxiliar);
        imagemProcessada = original;
        this.setPreferredSize(new Dimension(original.getWidth(), original.getHeight()));

    }//GEN-LAST:event_AbrirImagem1ActionPerformed

    private void adicionaPixelsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionaPixelsActionPerformed
        i = i+1;
        
        AbrirImagem1ActionPerformed(evt);
    }//GEN-LAST:event_adicionaPixelsActionPerformed

    private void botaoFeatureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoFeatureActionPerformed
        featureList.addAll(FeatureAssembly.montaFeaturePerImage(original, imagemAuxiliar));
        //System.out.println(featureList.size());
    }//GEN-LAST:event_botaoFeatureActionPerformed

    private void gerarArquivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gerarArquivoActionPerformed
        Utils.salvaArquivoTreinamento(featureList);
    }//GEN-LAST:event_gerarArquivoActionPerformed

    private void treinaRedeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_treinaRedeActionPerformed
        redeNeural = new RedeNeural();
        System.out.println("Feito");
    }//GEN-LAST:event_treinaRedeActionPerformed

    private void reconheceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reconheceActionPerformed
        try {
        //    captura.takeASnapShot();
            //imagemAuxiliar = Utils.reconheceImagem(original, redeNeural.getNeuralNetwork());
            //auxiliar.setImage(imagemAuxiliar);    
            captura.captureCam(this, redeNeural.getNeuralNetwork());
        } catch (IOException ex) {
            Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(JanelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }//GEN-LAST:event_reconheceActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JanelaPrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AbrirImagem1;
    private javax.swing.JButton adicionaPixels;
    private javax.swing.JButton botaoFeature;
    private javax.swing.JButton gerarArquivo;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JPanel painelAuxiliar;
    private javax.swing.JPanel painelPrincipal;
    private javax.swing.JButton reconhece;
    private javax.swing.JButton treinaRede;
    // End of variables declaration//GEN-END:variables
}
